package com.ruoyi.web.controller.common;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ComTenant;
import com.ruoyi.system.service.ComTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 租户Controller
 * 
 * @author tianl
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/com/comTenant")
public class ComTenantController extends BaseController {
    @Autowired
    private ComTenantService comTenantService;

    /**
     * 租户全部列表
     */
    @GetMapping(value = "/listAll")
    public AjaxResult listAll(ComTenant comTenant) {
        return success(comTenantService.list(comTenant));
    }

    /**
     * 查询租户列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ComTenant comTenant) {
        return getDataTable(comTenantService.listPage(comTenant));
    }

    /**
     * 导出租户列表
     */

    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComTenant comTenant) {
        List<ComTenant> list = comTenantService.list(comTenant);
        ExcelUtil<ComTenant> util = new ExcelUtil<>(ComTenant.class);
        util.exportExcel(response, list, "租户数据");
    }

    /**
     * 获取租户详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(comTenantService.selectById(id));
    }

    /**
     * 新增租户
     */

    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComTenant comTenant) {
        comTenantService.beforeSave(comTenant);
        return AjaxResult.success();
    }

    /**
     * 修改租户
     */

    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComTenant comTenant) {
        comTenantService.beforeSave(comTenant);
        return AjaxResult.success();
    }

    /**
     * 删除租户
     */

    @Log(title = "租户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(comTenantService.deleteByIds(Arrays.asList(ids)));
    }
}
